package com.hao.product.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hao.common.code.ErrorCode;
import com.hao.common.execption.BusinessException;
import com.hao.common.response.BaseResponse;
import com.hao.common.utils.ResultUtils;
import com.hao.product.app.annotation.LoginCheck;
import com.hao.product.app.model.dto.category.QueryCategoryRequest;
import com.hao.product.app.model.dto.category.UpdateCategoryRequest;
import com.hao.product.app.model.entity.Category;
import com.hao.product.app.service.CategoryService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;

/**
 * @author haoge
 * @version 1.0
 * @date 2023/5/23 15:51
 */
@RestController
@RequestMapping ("/category")
@Slf4j
@Api (tags = "商品类别管理")
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    @PostMapping ("/add")
    @LoginCheck
    public BaseResponse<Boolean> addCategory(String name) {
        if (StringUtils.isBlank(name)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Category category = new Category();
        category.setName(name);

        boolean result = categoryService.save(category);

        if (!result) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success(result);
    }

    @PostMapping ("/delete")
    @LoginCheck
    public BaseResponse<Boolean> deleteCategoryById(Long id) {
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = categoryService.removeById(id);

        if (!result) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success(result);
    }

    @PostMapping ("/update")
    @LoginCheck
    public BaseResponse<Boolean> updateCategoryById(@RequestBody @Valid UpdateCategoryRequest updateCategoryRequest) {
        if (updateCategoryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Category category = new Category();
        try {
            BeanUtils.copyProperties(category, updateCategoryRequest);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR);
        }
        boolean result = categoryService.updateById(category);
        if (!result) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success(result);
    }

    @GetMapping ("/list")
    @LoginCheck
    public BaseResponse<Page<Category>> listCategoryByPage(QueryCategoryRequest queryCategoryRequest) {
        if (queryCategoryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Page<Category> categoryPage = new Page<>(queryCategoryRequest.getPageNum(), queryCategoryRequest.getPageSize());
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.eq("id", queryCategoryRequest.getId());
        Page<Category> page = categoryService.page(categoryPage, categoryQueryWrapper);
        return ResultUtils.success(page);
    }
}
